package com.aoxin.controller.course.webCourse;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aoxin.mapper.SlfChildExpectedTimeMapper;
import com.aoxin.model.SlfChildExpectedTime;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aoxin.model.course.SlfChildren;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.service.inter.course.webCourse.ChildrenService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.EmptyUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;
import com.aoxin.util.log.LogInfo;

@RestController
@RequestMapping("/web/childrenController/")
public class ChildrenController {

	@Autowired
	ChildrenService childrenService;

	@Autowired
	SlfChildExpectedTimeMapper slfChildExpectedTimeMapper;

	/**
	 * 通过孩子id删除孩子
	 * 
	 * @param childrenId
	 * @param res
	 * @return
	 */
	@LogInfo(content = "孩子", operateType = OperateType.DELETE, userType = UserType.USER)
	@RequestMapping("deleteByChildrenId")
	public AjaxRes deleteByChildrenId(Integer childrenId, AjaxRes res) {
		if (childrenId != null) {
			Integer result = childrenService.deleteByChildrenId(childrenId);
			if (result != 0) {
				res.setSucceedMsg("deleteByChildrenId success!");
			} else {
				res.setFailMsg("deleteByChildrenId Fail!");
			}
		} else {
			res.setFailMsg("childrenId is null");
		}
		return res;
	}

	/**
	 * 新增孩子
	 * 
	 * @param slfChildren
	 * @param res
	 * @return
	 * @throws ParseException 
	 */
	@LogInfo(content = "孩子", operateType = OperateType.INSERT, userType = UserType.USER)
	@RequestMapping("insertChildren")
	public AjaxRes insertChildren(SlfChildren slfChildren, AjaxRes res,String parameter) throws ParseException {
		try {
			if (!EmptyUtil.objFieldsIsEmpty(slfChildren)) {
				Integer result = childrenService.insertChildren(slfChildren,parameter);
				if (result ==2) {
					res.setSucceedMsg(ErrorUtil.CHILDREN_GIVE);
				} else if(result>0){
					res.setSucceedMsg(ErrorUtil.CHILDREN_ADD);
				}
			} else {
				res.setFailMsg(ErrorUtil.CHILDREN_MUST);
			}
		} catch (IllegalAccessException e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 查询全部孩子|条件查询孩子
	 * 
	 * @param slfChildren
	 *            ,pageInfo
	 * @param res
	 * @return
	 */
	@LogInfo(content = "孩子", operateType = OperateType.QUERY)
	@RequestMapping("getChildren")
	public AjaxRes getChildren(SlfChildren slfChildren, AjaxRes res) {
		Map<String,Object> maps = new HashMap<>();
		List<String> oneList = new ArrayList<>();//星期一集合
		List<String> twoList = new ArrayList<>();//星期二集合
		List<String> threeList = new ArrayList<>();//星期三集合
		List<String> fourList = new ArrayList<>();//星期四集合
		List<String> fiveList = new ArrayList<>();//星期五集合
		List<String> sixList = new ArrayList<>();//星期六集合
		List<String> sevenList = new ArrayList<>();//星期日集合
		List<SlfChildExpectedTime> slfChildExpectedTimeList=slfChildExpectedTimeMapper.getSlfChildExpectedTimeByChildrenId(slfChildren.getChildrenId());
		for (SlfChildExpectedTime slfChildExpectedTime:slfChildExpectedTimeList){
			if(slfChildExpectedTime.getExpectedTime().contains("周一")){
				oneList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}else if(slfChildExpectedTime.getExpectedTime().contains("周二")){
				twoList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}else if(slfChildExpectedTime.getExpectedTime().contains("周三")){
				threeList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}else if(slfChildExpectedTime.getExpectedTime().contains("周四")){
				fourList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}else if(slfChildExpectedTime.getExpectedTime().contains("周五")){
				fiveList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}else if(slfChildExpectedTime.getExpectedTime().contains("周六")){
				sixList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}else if(slfChildExpectedTime.getExpectedTime().contains("周天")){
				sevenList.add(slfChildExpectedTime.getExpectedTime().substring(3));
			}
		}
		if(oneList != null && oneList.size() > 0){
			maps.put("1",oneList);
		}
		if(twoList != null && twoList.size() > 0){
			maps.put("2",twoList);
		}
		if(threeList != null && threeList.size() > 0){
			maps.put("3",threeList);
		}
		if(fourList != null && fourList.size() > 0){
			maps.put("4",fourList);
		}
		if(fiveList != null && fiveList.size() > 0){
			maps.put("5",fiveList);
		}
		if(sixList != null && sixList.size() > 0){
			maps.put("6",sixList);
		}
		if(sevenList != null && sevenList.size() > 0){
			maps.put("7",sevenList);
		}
		List<SlfChildren> slfChildrenList = childrenService.getChildren(slfChildren);
		for(SlfChildren slfChildren1:slfChildrenList){
			slfChildren1.setSlfChildExpectedTimeList(maps);
	}
		res.setSucceed(slfChildrenList, "getChildren success!");
		return res;
	}

	/**
	 * 修改孩子
	 * 
	 * @param slfChildren
	 * @param res
	 * @return
	 * @throws ParseException 
	 */
	@LogInfo(content = "孩子", operateType = OperateType.UPDATE, userType = UserType.USER)
	@RequestMapping("updateChildren")
	public AjaxRes updateChildren(SlfChildren slfChildren,String parameter,AjaxRes res) throws ParseException {
		try {
			if (!EmptyUtil.objFieldsIsEmpty(slfChildren)) {
				Integer result = childrenService.updateChildren(slfChildren,parameter);
				if (result != 0) {
					res.setSucceedMsg("updateChildren success!");
				} else {
					res.setFailMsg("updateChildren Fail!");
				}
			} else {
				res.setFailMsg("slfChildren is null");
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 查询用户的孩子ID姓名
	 * 
	 * @param res
	 * @return
	 */
	@RequestMapping("getChildrenIdName")
	public AjaxRes getChildrenIdName(AjaxRes res) {
		List<SelectDto> selectDtoList = childrenService.getChildrenIdName();
		res.setSucceed(selectDtoList, "getChildrenIdName success!");
		return res;
	}

}
